package com.sy.core.service;

import com.alibaba.fastjson.JSONObject;
import com.sy.core.ActiveMq.ActiveMqSendMessage;
import com.sy.core.mapper.SyProductAttributeMapper;
import com.sy.core.mapper.SyProductAttributeValueMapper;
import com.sy.core.mapper.SyProductMapper;
import com.sy.core.mapper.SyProductSkuMapper;
import com.sy.core.model.SyProduct;
import com.sy.core.model.SyProductAttribute;
import com.sy.core.model.SyProductAttributeValue;
import com.sy.core.model.SyProductSku;
import com.sy.core.web.BaseCtl;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.jms.Queue;
import java.util.List;
import java.util.Map;

@Service("productService")
public class ProductService extends BaseCtl {
    @Autowired
    private SyProductMapper syProductMapper;
    @Autowired
    private SyProductAttributeValueMapper syProductAttributeValueMapper;
    @Autowired
    private SyProductAttributeMapper syProductAttributeMapper;
    @Autowired
    private SyProductSkuMapper syProductSkuMapper;

    public List<Map<String,Object>> queyList(SyProduct syProduct){
        syProduct.setIsDelete("0");
        return syProductMapper.queryList(syProduct);
    }

    /**
     * 保存所有表的内容
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public String save(String syProductJson, String attrNameAndAttrValueJson, String syProductSkuJson) {
        SyProduct syProduct = JSONObject.parseObject(syProductJson,SyProduct.class);//主表数据
        String pk = syProduct.getPk();
        if(pk==null||"".equalsIgnoreCase(pk)){
            //保存主表数据
            //syProduct.setCreateBy();
            pk=getPrimaryKey();
            syProduct.setCreateTime(getTime());
            syProduct.setPk(pk);
            syProductMapper.insertSelective(syProduct);
        }else {
            syProductMapper.updateByPrimaryKeySelective(syProduct);
        }
        //先删除子表信息
        syProductAttributeMapper.deleteByProductPk(pk);
        syProductAttributeValueMapper.deleteByProductPk(pk);
        syProductSkuMapper.deleteByProductPk(pk);

        //获取skui信息并保存
        if(syProductSkuJson!=null){
            JSONObject syProductSkuJsonObj = JSONObject.parseObject(syProductSkuJson);
            List<SyProductSku> syProductSkuList=JSONObject.parseArray(syProductSkuJsonObj.getString("value"),SyProductSku.class);
            for (int i = 0; i <syProductSkuList.size() ; i++) {
                syProductSkuList.get(i).setPk(getPrimaryKey());
                syProductSkuList.get(i).setProductPk(pk);
                syProductSkuList.get(i).setCreaterId(getLoginUserPk());
                syProductSkuList.get(i).setCreaterName(getLoginUser().getName());
                syProductSkuList.get(i).setCreateTime(getTime());
                syProductSkuMapper.insertSelective( syProductSkuList.get(i));
            }
        }

        //获取属性和属性值信息并保存
        if(attrNameAndAttrValueJson!=null){
            JSONObject attrNameAndAttrValueJsonObj = JSONObject.parseObject(attrNameAndAttrValueJson);
            List<JSONObject> jsonObjectList =JSONObject.
                    parseArray(attrNameAndAttrValueJsonObj.getString("value"),JSONObject.class);
            for (int i = 0; i <jsonObjectList.size() ; i++) {
                //获取属性名称列表
                SyProductAttribute syProductAttribute = new SyProductAttribute();
                syProductAttribute.setAttrName((String)jsonObjectList.get(i).get("attrName"));
                String attrPk = getPrimaryKey();
                syProductAttribute.setPk(attrPk);
                syProductAttribute.setProductPk(pk);
                syProductAttribute.setCreaterId(getLoginUser().getPk());
                syProductAttribute.setCreaterName(getLoginUser().getName());
                syProductAttribute.setCreateTime(getTime());
                syProductAttributeMapper.insertSelective(syProductAttribute);//插入属性名称
                SyProductAttributeValue syProductAttributeValue = new SyProductAttributeValue();
                List<String> syProductAttributeValueList = (List<String>) jsonObjectList.get(i).get("attrValue");
                for (int j = 0; j <syProductAttributeValueList.size() ; j++) {
                    String attrVluePk=getPrimaryKey();
                    syProductAttributeValue.setPk(attrVluePk);
                    syProductAttributeValue.setAttrPk(attrPk);
                    syProductAttributeValue.setAttrName((String)jsonObjectList.get(i).get("attrName"));
                    syProductAttributeValue.setProductPk(pk);
                    syProductAttributeValue.setAttrValue(syProductAttributeValueList.get(j));
                    syProductAttributeValueMapper.insertSelective(syProductAttributeValue);
                }
            }

        }
        return pk;
    }

    /**
     * 查询主表数据
     * @param pk
     * @return
     */
    public Map<String,Object> queryOne(String pk) {
        return syProductMapper.selectByPrimaryKeyForMap(pk);
    }

    public int batchDel(String pks) {
        String[] pkList = pks.split(",");
        for (int i = 0; i <pkList.length ; i++) {
            //先删除子表信息
            syProductSkuMapper.deleteByProductPk(pkList[i]);
        }
        return syProductMapper.batchDel(pks);

    }
}
